package com.cym.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cym.service.DatabaseService;
import com.cym.utils.BaseController;
import com.cym.utils.JsonResult;

import cn.hutool.core.util.StrUtil;

@Controller
@RequestMapping("/transTable")
public class TransTableController extends BaseController {

	@Autowired
	DatabaseService databaseService;

	@RequestMapping("")
	public ModelAndView main(ModelAndView modelAndView, String keyword) {

		modelAndView.addObject("fromBase", databaseService.getByType(null));

		modelAndView.setViewName("/transTable/index");
		return modelAndView;
	}

	@ResponseBody
	@RequestMapping("/build")
	public JsonResult getCode(String fromBaseId, String fromTable, String toTable, String mainKey) {
		if (StrUtil.isEmpty(mainKey)) {
			mainKey = "_id";
		}
		String sql = databaseService.buildSql(fromBaseId, fromTable, toTable, mainKey);

		String sqlDel = databaseService.buildSqlDel(fromTable);

		Map map = new HashMap();
		map.put("sql", sql);
		map.put("sqlDel", sqlDel);

		return renderSuccess(map);
	}

}
